package com.example.cet4try.activity;

import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import com.example.cet4try.R;
import com.example.cet4try.Utils.Tools;
import com.example.cet4try.Utils.WordData;
import com.example.cet4try.myDialog.WordDetailsDialog;

public class ActivityChineseEnglishTest extends BaiduTTSActivity {

    private Button button_en1;
    private Button button_en2;
    private Button button_en3;
    private Button button_en4;
    private TextView chinese_show1;
    private int dui;
//    private String[] data;
    private WordData wordData;
    private boolean endflag = false;
    private String username;
//    private TextToSpeech textToSpeech; // TTS对象
    private WordDetailsDialog wordDetailsDialog;
    private boolean wrongFlag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cnen_test);

        button_en1 = findViewById(R.id.button_en1);
        button_en2 = findViewById(R.id.button_en2);
        button_en3 = findViewById(R.id.button_en3);
        button_en4 = findViewById(R.id.button_en4);
        chinese_show1 = findViewById(R.id.chinese_show1);
        wordDetailsDialog = new WordDetailsDialog("中英测试");
        wordDetailsDialog.setPlaySound(new WordDetailsDialog.PlaySound() {
            @Override
            public void speak_d() {
                speak();
            }
        });
        button_en1.setOnClickListener(new ActivityChineseEnglishTest.button_cn_Onclick());
        button_en2.setOnClickListener(new ActivityChineseEnglishTest.button_cn_Onclick());
        button_en3.setOnClickListener(new ActivityChineseEnglishTest.button_cn_Onclick());
        button_en4.setOnClickListener(new ActivityChineseEnglishTest.button_cn_Onclick());
        Button next = findViewById(R.id.next_cn);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reStart();
            }
        });

        SharedPreferences preferences = getSharedPreferences("App_Shared_Data", 0);
        username = preferences.getString("username","");

        int id = Tools.MyRandom(1,4493);//1-4493
        wordData = Tools.souID(id);
        Tools.AddUserData(username,wordData.getMid(),"learned");

        // 按钮控制开始朗读
        Button speechBtn = findViewById(R.id.langdux);
        speechBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                speak();
            }
        });

        Button btn_rs = findViewById(R.id.button5);
        btn_rs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tools.AddUserData(username,wordData.getMid(),"already");
                reStart();
            }
        });

        String chinese = Tools.GetRandomdata(wordData.getChinese());
        chinese_show1.setText(chinese);
        dui = Tools.MyRandom(1,4);
        setShowText(dui,wordData.getEnglish());

        for (int i = 1;i<=4;i++){
            if (i!=dui){
                int iderror = Tools.MyRandom(1,4493);
                String str = Tools.souID(iderror).getEnglish();
                setShowText(i,str);
            }

        }

    }

    private void setShowText(int index,String str){
        switch (index){
            case 1:button_en1.setText(str);break;
            case 2:button_en2.setText(str);break;
            case 3:button_en3.setText(str);break;
            case 4:button_en4.setText(str);break;
            default:break;
        }
    }


    private void speak() {
        super.speak(wordData.getEnglish());
    }

    private class button_cn_Onclick implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            if (endflag){
                return;
            }
            AddTimes();
            Button button = findViewById(R.id.button_en1);
            int btn = 0;
            switch (view.getId()){
                case R.id.button_en1:btn=1;button =  findViewById(R.id.button_en1);break;
                case R.id.button_en2:btn=2;button =  findViewById(R.id.button_en2);break;
                case R.id.button_en3:btn=3;button =  findViewById(R.id.button_en3);break;
                case R.id.button_en4:btn=4;button =  findViewById(R.id.button_en4);break;
                default:break;
            }

            if (dui == btn){
                endflag = true;
                if (!wrongFlag)
                    AddRight();
                Drawable yesicon = getResources().getDrawable(R.drawable.yes);
                yesicon.setBounds(0,0,150,150);
                button.setCompoundDrawables(null, null, yesicon, null);
                wordDetailsDialog.setData(wordData);
                wordDetailsDialog.show(getSupportFragmentManager(),"WordDetailsDialog");

            }
            else {
                wrongFlag = true;
                Animation shake = AnimationUtils.loadAnimation(ActivityChineseEnglishTest.this,R.anim.shake);
                button.startAnimation(shake);

                Tools.AddUserData(username,wordData.getMid(),"wrong");

            }


        }
    }


}
